<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="240px" class="sidebar">
        <div class="logo">
          <h3>智慧养老系统</h3>
          <span>管理端</span>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          :router="true"
        >
          <el-menu-item index="/admin/dashboard">
            <el-icon><Odometer /></el-icon>
            <span>数据概览</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/elderly">
            <el-icon><User /></el-icon>
            <span>老人信息管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/health">
            <el-icon><Document /></el-icon>
            <span>健康数据记录</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/appointments">
            <el-icon><Calendar /></el-icon>
            <span>服务预约管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/staff">
            <el-icon><Avatar /></el-icon>
            <span>护理人员管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ breadcrumbTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-icon><Avatar /></el-icon>
                {{ userInfo.username }}
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区域 -->
        <el-main class="main-content">
          <RouterView />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Odometer, 
  User, 
  Document, 
  Calendar, 
  Avatar,
  ArrowDown
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const userInfo = ref({
  username: '',
  role: ''
})

const activeMenu = computed(() => route.path)

const breadcrumbTitle = computed(() => {
  const routeMap: { [key: string]: string } = {
    '/admin/dashboard': '数据概览',
    '/admin/elderly': '老人信息管理',
    '/admin/health': '健康数据记录',
    '/admin/appointments': '服务预约管理',
    '/admin/staff': '护理人员管理'
  }
  return routeMap[route.path] || '首页'
})

const handleCommand = (command: string) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      localStorage.removeItem('userInfo')
      ElMessage.success('退出成功')
      router.push('/login')
    })
  } else if (command === 'profile') {
    ElMessage.info('个人信息功能待开发')
  }
}

onMounted(() => {
  const savedUserInfo = localStorage.getItem('userInfo')
  if (savedUserInfo) {
    userInfo.value = JSON.parse(savedUserInfo)
  }
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  min-height: 100vh;
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #434c5e;
  margin-bottom: 0;
}

.logo h3 {
  color: #fff;
  margin: 0 0 5px 0;
  font-size: 18px;
}

.logo span {
  color: #bfcbd9;
  font-size: 12px;
}

.admin-menu {
  border: none;
}

.admin-menu .el-menu-item .el-icon {
  transform: translateY(2%);
}

.header {
  background: #fff;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
  font-size: 14px;
}

.user-info .el-icon {
  margin: 0 5px;
}

.main-content {
  background: #f0f2f5;
  padding: 20px;
  min-height: calc(100vh - 60px);
}
</style> 